package com.chinacoal.hr.levelimpl.service.impl;

import com.chinacoal.hr.levelimpl.dao.AttendanceAppealDao;
import com.chinacoal.hr.levelimpl.dao.BizTravelDao;
import com.chinacoal.hr.levelimpl.dao.LeaveRecordDao;
import com.chinacoal.hr.levelimpl.service.AttendanceTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * 考勤下的任务服务
 *
 * @author: YunTao.Li
 * @create: 2020-06-07 10:17
 * @description:
 **/
@Service
public class AttendanceTaskServiceImpl implements AttendanceTaskService {

    @Autowired
    private LeaveRecordDao leaveRecordDao;
    @Autowired
    private BizTravelDao bizTravelDao;
    @Autowired
    private AttendanceAppealDao attendanceAppealDao;

    //申请单类型：协同平台-考勤-请假单
    public static final String BIZCODE_HR_LEAVE = "ccms_hr_leave";
    //申请单类型：协同平台-考勤-因公外出申请单
    public static final String BIZCODE_HR_TRAVEL = "ccms_hr_travel";
    //申请单类型：协同平台-考勤-考勤申诉单
    public static final String BIZCODE_HR_APPEAL = "ccms_hr_appeal";

    /**
     * 根据业务类型与业务ID获得业务详情对象
     *
     * @param bizID   :
     * @param bizCode :
     * @return : java.util.Map<java.lang.String,java.lang.String>
     * @author : YunTao.Li
     * @date : 2020/6/7 2020/6/7
     */
    public Map<String, String> getAttendanceTaskInfo(String bizID, String bizCode) throws Throwable {
        Object vo = null;
        Map<String, String> condition = new HashMap<String, String>();

        if (BIZCODE_HR_LEAVE.equals(bizCode)) { // 请假单

            condition.put("leaveId", bizID);
            vo = this.leaveRecordDao.getLeaveRecord(condition);
        } else if (BIZCODE_HR_TRAVEL.equals(bizCode)) { // 因公外出单

            condition.put("bizTravelId", bizID);
            vo = this.bizTravelDao.getBizTravelInfo(condition);
        } else if (BIZCODE_HR_APPEAL.equals(bizCode)) { // 考勤申诉单

            condition.put("explainId", bizID);
            vo = this.attendanceAppealDao.getAttendanceAppeal(condition);
        } else {

        }

        Map recordMap = transBean2Map(vo);
        return recordMap;
    }

    public static Map<String, Object> transBean2Map(Object obj) {

        if (obj == null) {
            return null;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();

                // 过滤class属性
                if (!key.equals("class")) {
                    // 得到property对应的getter方法
                    Method getter = property.getReadMethod();
                    Object value = getter.invoke(obj);

                    map.put(key, value);
                }
            }
        } catch (Exception e) {
            System.out.println("transBean2Map Error " + e);
        }

        return map;
    }
}
